<template>
  <section>
    <div class="filter-container">
      <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="分类名称"
        v-model="listQuery.keyword" />
      <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
    </div>
    <el-table max-height="500" :data="list" style="width: 100%">
      <el-table-column align="left" label="分类名称">
        <template slot-scope="scope">
          <span>{{ scope.row.typeName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div v-show="!listLoading" class="pagination-container">
              <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[20,30, 50]" :page-size="listQuery.num" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
          </div> -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      width="500px" @close="dialogClose">
      <el-form :model="form" :rules="rules" ref="form" label-width="150px">
        <el-form-item label="分类名称" prop="typeName">
          <common-input v-model="form.typeName" placeholder="请输入分类名称" maxlength="10" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <common-input v-model="form.sort" class="w60" maxlength="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancel('form')">取消</el-button>
        <el-button size="mini" v-if="dialogStatus == 'create'" type="primary" @click="create('form')">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="update('form')">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import {
  isIntegerNotMust
} from '../../utils/validator'
import {
  courseTypeSave,
  courseTypeDelete,
  courseType
} from '@/api/setting';
export default {
  components: {
  },
  data() {
    return {
      list: [],
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        keyword: undefined
      },
      total: null,
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        update: '编辑分类',
        create: '新增分类'
      },
      form: {
        id: undefined,
        typeName: undefined,
        sort: 0,
        remark: ''
      },
      rules: {
        typeName: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],
        sort: [
          {
            validator: isIntegerNotMust,
            trigger: 'blur'
          }
        ]
      },
    }
  },
  mounted() {
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.listLoading = true;
      courseType(this.listQuery).then(response => {
        this.list = response.data.list;
        this.listLoading = false;
      })
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    resetTemp() {
      this.form = {
        name: undefined,
        id: undefined,
        sort: 0,
      };
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (!this.form.sort) {
            this.form.sort = 0;
          }
          courseTypeSave(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$message.success("分类创建成功");
            })
        } else {
          return false;
        }
      });
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          if (!this.form.sort) {
            this.form.sort = 0;
          }
          this.dialogFormVisible = false;
          this.form.password = undefined;
          courseTypeSave(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$message.success("分类更新成功");
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    handleUpdate(row) {
      // qa_cat_detail(row.id)
      //   .then(response => {
      this.form = row;
      this.dialogFormVisible = true;
      this.dialogStatus = 'update';
      // });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          courseTypeDelete(row.id)
            .then(() => {
              this.$message.success("分类删除成功");
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        }).catch(() => { });
    },
    dialogClose() {
      this.$refs.form.resetFields();
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.num = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>
